package com.example.nio.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.nio.commons.Agent;
import com.example.nio.commons.ChatMessage;
import com.example.nio.commons.Message;

import java.util.concurrent.CyclicBarrier;

public class WsChange {
	CyclicBarrier barrier = new CyclicBarrier(4);

	/**
	 * JSON转成客服（Agent）对象
	 *
	 * @param content
	 * @return
	 */
	public Agent agentChange(String content) {
		Agent agent = new Agent();
		JSONObject jsonObject = JSONUtil.parseObj(content);
		String state = jsonObject.getStr("state");
		agent.setAgent(JSONUtil.parseObj(state).getStr("agentId"));
		agent.setDepartment(JSONUtil.parseObj(state).getStr("department"));
		agent.setState(0);
		agent.setCheckUser(0);
		return agent;
	}

	/**
	 * JSON转成信息(Message)对象
	 *
	 * @param content
	 * @return
	 */
	public Message messageChange(String content) {
		Message message = Convert.convert(Message.class,content);
		if(message.getContentType()==null) {
			message.setContentType(1);
		}
		return message;
	}

}
